package com.itheima.controller;

import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//被扫描
//返回的数据结果 = json
//切面
@RestControllerAdvice
public class ExceptionAdviceHandler {

    //路径（拦截的异常）
    @ExceptionHandler(BusinessException.class)
    //方法
    public Result exception(BusinessException e){
        System.out.println("BusinessException = " + e.getMessage());
        e.printStackTrace();
        return new Result(e.getCode(),null,e.getMessage());
    }

    //路径（拦截的异常）
    @ExceptionHandler(SystemException.class)
    //方法
    public Result exception(SystemException e){
        System.out.println("SystemException = " + e.getMessage());
        e.printStackTrace();
        return new Result(e.getCode(),null,e.getMessage());
    }

    //路径（拦截的异常）
    @ExceptionHandler(Exception.class)
    //方法
    public Result exception(Exception e){
        System.out.println(e.getMessage());
        e.printStackTrace();
        return new Result(Code.ERR_ERR,null,"这是个好问题，好在哪呢，好在我也不知道");
    }

    //dao 异常捕获异常  DataIntegrityViolationException
    @ExceptionHandler(DataIntegrityViolationException.class)
    public Result dive(DataIntegrityViolationException e){
        String msg = "我们间有一个出现了毛病，尼采一下?";
        if (e.getMessage().contains("Data too long")){
             msg = "你下面都没你输入的这么长！！！！";
        }
        return new Result(Code.DAO_ERR,null,msg);
    }
}
